/**
 * 留资信息页面
 * - 使用自定义封装组件，简化代码，统一管理。
 * - 原文件备份：./index_backup.js
 * - Edit弹窗暂未调整，沿用旧版
 * - 2025-07-11 09:36:43 tzj
 * */

import {lzApi} from "../../services/basic";
import EditItem from "./EditItem";
import React from "react";
import Dictionary from "../../services/dictionary";
import withBasePage from "../../components/BaseComponent/withBasePage";
import DelModal from "../../components/delModal";
import {message} from "antd";

// 要显示的form表单
const formItemList =
    ["fuzzy", "companies", "followState", "accounts",
        {
            name: "dateRange",
            label: '留资日期'
        },
        "serviceWx", "serviceDy", "channel", "jhSelect", 'stateFlag2', 'period'];
// 显示的按钮
const buttons = ["query", "add", "edit", "export", "delete"];
// 表格列信息
const columns = [
    {title: '留资编号', dataIndex: 'lzbh'},
    {title: '联系方式', dataIndex: 'lxfs'},
    {title: '车型', dataIndex: 'cx'},
    {title: '区域', dataIndex: 'qy'},
    {title: '导购', dataIndex: 'dgmc'},
    {title: '意向项目', dataIndex: 'yxxmmc'},
    // 标识：lxrmc，名称：kfxm
    {title: '微信客服', dataIndex: 'kfxm'},
    // 标识：lxrmc，名称：cjrxm
    {title: '抖音客服', dataIndex: 'cjrxm'},
    {title: '跟进状态', dataIndex: 'gjztmc'},
    {title: '公司名称', dataIndex: 'yymc'},
    {title: '留资日期', dataIndex: 'lzrq'},
    {title: '来源账号', dataIndex: 'hzxm'},
    {title: '计划名称', dataIndex: 'jhmc'},
    {title: '创建时间', dataIndex: 'cjsj'},
    {
        title: '响应时间', dataIndex: 'gjsj',
        render(text, record) {
            const colors = Dictionary.responseColors;
            const i = Number(record["xyzt"])
            return <span style={{color: colors[i]}}>{text}</span>
        },
        excelRender: text => text,
        excelFont: (_, record) => {
            const str = Dictionary.responseColors[Number(record["xyzt"])];
            const argb = str.startsWith("#") ? str.slice(1) : str;
            return {
                bold: true,
                // size: 18,
                color: {
                    argb
                }
            }
        }
    },
    {title: '留资时段', dataIndex: 'sjlx'},
    {
        title: '有效性', dataIndex: 'ztbz', width: "4.5em",
        render: (text) => Dictionary.validity[text]
    },
];

// 用于旧数据字典
const dataBook = {
    83: 'gjzt', // 科室类型
    84: 'lxly', // 公共科室
    85: 'yxxm', // 公共科室
}

// 实际执行还是在class里，而非视为function
const modalRef = React.createRef()

const confirmCb = (data, record, hoc) => {
    const path = `${record.id}&${data.zfyybh}&${data.zfyy}`
    lzApi.delete(path).then(res => {
        if (res.code === "1") {
            message.success("操作成功")
            modalRef.current.close();
            hoc.queryDataList();
        } else {
            message.warn(res.msg || "操作失败")
        }
    }).catch((err) => message.warn(err.msg || "操作失败"))
}

export default withBasePage({
    state: {
        formItemList,
        buttons,
        // columns,
        filename: "留资信息",
        // tableTitle: true,
        params: {
            // ztbz: "1"
        },
        rowKey: "lzid",
        // 自定义删除事件
        deleteCustom: true,
        dataBook
    },
    defaultPageSize: 7,
    columns,
    queryApi: lzApi.getPage,
    // deleteApi : lzApi.delete,
    EditItem,
    deleteItem(record) {
        console.log(record)
        modalRef.current.open("留资编号：" + record.lzbh, record)
    },
    // extraContentBeforeTable: (hoc) => <StopBtn hoc={hoc}/>,
    extraContentAfterTable: (hoc) => <DelModal confirmCb={(data, record) => confirmCb(data, record, hoc)}
                                               ref={modalRef}></DelModal>,
})(() => null);